﻿using Microsoft.Xna.Framework.Content;
using Imaginecup.util.resourceMan;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.factory;
using Microsoft.Xna.Framework;
using Imaginecup.util;
using Imaginecup.sprite.animation;
using Imaginecup.sprite;
using imaginecup.util;
using System.Collections.Generic;
using Imaginecup.util.rainEffect;
using Imaginecup.gameState.intro;
using Imaginecup.scroll;

namespace Imaginecup.gameState.ending.unuse
{
    //
    public class GameEnding1 : GameState
    {
        enum EndingState { Scene1, Scene2, Cloud, Scene3, Scene4, Scene5 };

        private ContentManager m_Content;
        private static GameEnding1 m_Ending = null;

        private ScrollMan m_ScrollMan;

        private CloudScene m_CloudScene;

        private EndingScene1 m_Scene1;
        private EndingScene2 m_Scene2;
        private EndingScene3 m_Scene3;
        private EndingScene4 m_Scene4;
        private EndingScene5 m_Scene5;

        private EndingState m_State;

        private GameEnding1()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content";
            m_CloudScene = new CloudScene();
            m_ScrollMan = new ScrollMan();
        }

        public static GameEnding1 GetEnding()
        {
            if (m_Ending == null)
            {
                m_Ending = new GameEnding1();
            }
            return m_Ending;
        }

        /************************************************************************/
        /* 초기화 되는 값                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            m_Scene1 = new EndingScene1();
            m_Scene2 = new EndingScene2();
            m_Scene3 = new EndingScene3();
            m_Scene4 = new EndingScene4();
            m_Scene5 = new EndingScene5();

            m_State = EndingState.Scene1;

            int maxY = 480 * 8; // 화면사이즈 * 8장( 총 9장이므로 마지막장 최상단 좌표는480*8)
            m_ScrollMan.SetScrollProcess(new HeightDownScroll(0, 80, maxY));
            m_ScrollMan.StartScroll();

        }

        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            ResourceMan.GetResourceMan().SetTextureFrame("BlackBG", m_Content.Load<Texture2D>("ending\\1\\1"));

            NormalSprite sprite = new NormalSprite();
            sprite.SetTextureName("BlackBG");

            m_ScrollMan.SetSprite( sprite );

            m_Scene1.LoadEndingScene();
            m_Scene2.LoadEndingScene();
        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            m_Content.Unload();
            ResourceMan.GetResourceMan().Release();
        }
        

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();
            
            if(mouseInput.IsOneClick())
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Select));
                return;
            }


            if (m_State == EndingState.Cloud)
                m_CloudScene.Update(gameTime);

          
            m_Scene1.Update(gameTime, m_ScrollMan);
            if (m_ScrollMan.GetCurrentStartPos().Y > 300 && m_State == EndingState.Scene1) //  480 + 480/2 중앙에서 애니메이션 시작
            {
                m_Scene3.LoadEndingScene();
                m_Scene2.StartAnimation();
                m_State = EndingState.Scene2;
            }
            m_Scene2.Update(gameTime , m_ScrollMan);
            
            if( m_ScrollMan.GetCurrentStartPos().Y > 670 && m_State == EndingState.Scene2 )
            {
                m_Scene4.LoadEndingScene();
                m_Scene3.StartAnimation();
                m_State = EndingState.Scene3;
            }
            m_Scene3.Update(gameTime, m_ScrollMan);

            if( m_ScrollMan.GetCurrentStartPos().Y > 1300 && m_State == EndingState.Scene3 )
            {
                m_Scene4.StartAnimation();
                m_State = EndingState.Scene4;
                m_Scene5.LoadEndingScene();
            }
            m_Scene4.Update(gameTime, m_ScrollMan);

            if( m_ScrollMan.GetCurrentStartPos().Y > 1500 && m_State == EndingState.Scene4)
            {
                m_Scene5.StartAnimation();
                m_State = EndingState.Scene5;
            }
            m_Scene5.Update(gameTime, m_ScrollMan);

            m_ScrollMan.Update(gameTime);
            base.Update(gameTime);
        }

       
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();

            m_ScrollMan.Draw(sb);

            m_Scene1.Draw(sb);
            m_Scene2.Draw(sb);
            m_Scene3.Draw(sb);
            m_Scene4.Draw(sb);
            m_Scene5.Draw(sb);

            if( m_State == EndingState.Cloud)
                m_CloudScene.Draw(sb);
            base.Draw(gameTime);
        }
    }
}
